﻿#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif

#include "MainPage.xaml.h"

namespace winrt::NovelReader::implementation
{
	uint64_t MainWindow::s_idValue {}; 

	void MainWindow::InitializeComponent()
	{
		MainWindowT::InitializeComponent(); 

		// Set titlebar
		this->ExtendsContentIntoTitleBar(true); 

		// Maximize Window
		auto presenter =  AppWindow().Presenter(); 
		auto overlappedPresenter = presenter.as<Microsoft::UI::Windowing::OverlappedPresenter>(); 
		overlappedPresenter.Maximize(); 

		// UI Elements
		m_titleBar = winrt::NovelReader::TitleBarControl();
		m_titleBarViewModel = m_titleBar.ViewModel(); 
		winrt::Microsoft::UI::Xaml::Controls::Grid::SetRow(m_titleBar, 0); 

		m_mainPage = winrt::NovelReader::MainPage(); 
		m_mainPageViewModel = m_mainPage.ViewModel(); 
		winrt::Microsoft::UI::Xaml::Controls::Grid::SetRow(m_mainPage, 1); 

		this->RootGrid().Children().Append(m_titleBar); 
		this->RootGrid().Children().Append(m_mainPage); 

		// Events
		m_titleBarViewModel.ScrollDirectionChanged({&m_mainPage, &winrt::NovelReader::MainPage::ScrollReader}); 
		m_titleBarViewModel.ContentsVisibilityChanged({&m_mainPageViewModel, &winrt::NovelReader::MainPageViewModel::SetContentsVisibility});
		m_titleBarViewModel.AutoTurnPageEvent({&m_mainPage, &winrt::NovelReader::MainPage::EnableAutoTurnPage}); 
		m_mainPageViewModel.DisplayTitleChanged({ &m_titleBarViewModel, &winrt::NovelReader::TitleBarViewModel::SetDisplayTitle });

	}

	uint64_t MainWindow::IdValue()
	{
		return s_idValue; 
	}

	void MainWindow::Window_Activated(
		winrt::Windows::Foundation::IInspectable const&, winrt::Microsoft::UI::Xaml::WindowActivatedEventArgs const&)
	{
		if (s_idValue == 0) {
			s_idValue = this->AppWindow().Id().Value; 
		}
	}
}


